﻿using Espam.Idiomas.Decimo.Tesis.Negocio;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Espam.Idiomas.Decimo.Tesis.Web
{
    public partial class UI_ListaModulos : System.Web.UI.Page
    {
        string validaSession = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {


                Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
                Response.Cache.SetAllowResponseInBrowserHistory(false);
                Response.Cache.SetNoStore();

                if (Session["userEspamIdiomasVarSession001"] == null || Session["rolEspamIdiomasVarSession003"] == null)
                {
                    validaSession = "NoHaIniciadoSession";
                    Response.Redirect("UI_Login.aspx?validaSession=" + validaSession);
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Docente")
                {
                    Response.Redirect("UI_DashboardTeacher.aspx");
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Administradores")
                {
                    //btnGuardarModulo.Attributes.Add("onclick", " this.disabled = true,this.value='Procesando...'; " + ClientScript.GetPostBackEventReference(btnGuardarModulo, null) + ";");
                    //Attributes.Add("OnClick", "this.value='Procesando...'");

                    //CargarComboboxes();
                    CargaGrid();
                    lblvalida.Text = "";
                    lblvalida.Visible = false;
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Alumno")
                {
                    Response.Redirect("UI_DashboardStudents.aspx");
                }

            }
            else
            {
                lblvalida.Text = "";
                lblvalida.Visible = false;
            }
            MaintainScrollPositionOnPostBack = true;

        }
        private void CargaGrid()
        {
            //ListadoHorarios olistadoHorarios = new ListadoHorarios();
            //gvHorarios.DataSource = olistadoHorarios.getHorariosGirdview();
            //gvHorarios.DataBind();
            Modulos om = new Modulos();
            GridView1.DataSource = om.getHorariosAndModulos();
            GridView1.DataBind();

        }
        private void CrearReporte()
        {

            EspamIdiomasDataSet.sp_getModulosAndHorariosDataTable dt = new EspamIdiomasDataSet.sp_getModulosAndHorariosDataTable();
            EspamIdiomasDataSetTableAdapters.sp_getModulosAndHorariosTableAdapter da = new EspamIdiomasDataSetTableAdapters.sp_getModulosAndHorariosTableAdapter();
            da.Fill(dt);

            ReportViewer viewer = new ReportViewer();
            ReportDataSource RD = new ReportDataSource();
            RD.Value = dt;
            RD.Name = "DataSet1";
            //viewer.LocalReport.ReportPath = Server.MapPath("Reports/Administradores/ReporteModulosPorHorarioCompleto.rdlc");
            viewer.LocalReport.DataSources.Clear();
            viewer.LocalReport.DataSources.Add(RD);
            //viewer.LocalReport.ReportEmbeddedResource = "ReporteModulosPorHorarioCompleto.rdlc";
            viewer.LocalReport.ReportPath = Server.MapPath("Reports/Administradores/ReporteModulosPorHorarioCompleto.rdlc");
            viewer.LocalReport.Refresh();

            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;
            byte[] pdfContent = viewer.LocalReport.Render("PDF", string.Empty, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            this.Response.Clear();
            this.Response.ContentType = "application/pdf";
            this.Response.AddHeader("Content-disposition", "attachment; filename=ReporteModulosPorHorarioCompleto.pdf");
            this.Response.BinaryWrite(pdfContent);
            this.Response.End();
        }

        protected void btnGuardarModulo_Click(object sender, EventArgs e)
        {
            CrearReporte();
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("UI_ListaModulos.aspx");
        }

        protected void GridView1_Load(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count > 0)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Dttable", String.Format("CargaDataTable('{0}',{1});", "Contenido_GridView1", GridView1.Rows[0].Cells.Count), true);
        }

        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            GridView GridView1 = (sender) as GridView;
            if (GridView1.Rows.Count > 0)
            {
                //This replaces <td> with <th> and adds the scope attribute
                GridView1.UseAccessibleHeader = true;

                //This will add the <thead> and <tbody> elements
                GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;

                //This adds the <tfoot> element. 
                //Remove if you don't have a footer row
                GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
            }
        }
    }
}